﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using System.Drawing;
using AwesomeGame.Weapons.Pickups;

namespace AwesomeGame.Map
{
    public class Level
    {
        public readonly int TILE_SIZE = 64;
        public TileTypes[,] Tiles { get; set; }
        public int Width;
        public int Height;

        private Texture2D _wallTileImage;
        public List<WeaponPickup> WeaponPickups = new List<WeaponPickup>();
        private List<Vector2> _spawnPoints = new List<Vector2>();
        public List<Teleporter> Teleporters = new List<Teleporter>();
        private List<DestructableWorldItem> DestructableWorldItems = new List<DestructableWorldItem>();

        public Level(string name)
        {
            _wallTileImage = Game1.ContentManager.Load<Texture2D>(@"Terrain\Wall");
            LoadMap(name);
        }

        private void LoadMap(string name)
        {
            Bitmap levelImage = (Bitmap)Bitmap.FromFile(name);
            Width = levelImage.Width;
            Height = levelImage.Height;

            Tiles = new TileTypes[Width, Height];

            for (int y = 0; y < Height; y++)
                for (int x = 0; x < Width; x++)
                {
                    if (levelImage.GetPixel(x, y).Name == "ff000000")
                        Tiles[x, y] = TileTypes.Wall;
                    else if (levelImage.GetPixel(x, y).Name == "ffffffff")
                        Tiles[x, y] = TileTypes.Floor;
                    else if (levelImage.GetPixel(x, y).Name == "ffff6500")
                        WeaponPickups.Add(new WeaponPickup(Weapons.WeaponType.BouncyBullet, new Vector2(x * TILE_SIZE, y * TILE_SIZE)));
                    else if (levelImage.GetPixel(x, y).Name == "ff0024ff")
                        WeaponPickups.Add(new WeaponPickup(Weapons.WeaponType.Bullet, new Vector2(x * TILE_SIZE, y * TILE_SIZE)));
                    else if (levelImage.GetPixel(x, y).Name == "ff84ff00")
                        WeaponPickups.Add(new WeaponPickup(Weapons.WeaponType.Rocket, new Vector2(x * TILE_SIZE, y * TILE_SIZE)));
                    else if (levelImage.GetPixel(x, y).Name == "ff9a5d5d")
                        _spawnPoints.Add(new Vector2(x, y));
                    else if (levelImage.GetPixel(x, y).Name == "ff05526e")
                        Teleporters.Add(new Teleporter(new Vector2(x, y), new Vector2(x * TILE_SIZE, y * TILE_SIZE)));
                    else if (levelImage.GetPixel(x, y).Name == "ff5d9a71")
                        DestructableWorldItems.Add(new DestructableWorldItem(new Vector2(x, y), new Vector2(x * TILE_SIZE, y * TILE_SIZE), 10));
                    else
                        throw new Exception("Invalid color detected in level file: " + levelImage.GetPixel(x, y).Name);
                }
        }

        public Vector2 ConvertWorldPositionToTileIndex(Vector2 worldPosition)
        {
            return new Vector2((int)(worldPosition.X / TILE_SIZE), (int)(worldPosition.Y / TILE_SIZE));
        }

        public TileTypes GetTileAtWorldPosition(Vector2 worldPosition)
        {
            return Tiles[(int)(worldPosition.X / TILE_SIZE), (int)(worldPosition.Y / TILE_SIZE)];
        }

        public DestructableWorldItem GetDestructableItemAtWorldPosition(Vector2 worldPosition)
        {
            Vector2 index = new Vector2((int)(worldPosition.X / TILE_SIZE), (int)(worldPosition.Y / TILE_SIZE));
            foreach(DestructableWorldItem item in DestructableWorldItems)
                if (item.TilePosition == index)
                    return item;
            return null;
        }

        public void Draw(GameTime gameTime)
        {
            foreach (WeaponPickup pickup in WeaponPickups)
                pickup.Draw(gameTime);
            foreach (DestructableWorldItem item in DestructableWorldItems)
                item.Draw(gameTime);
            foreach (Teleporter teleporter in Teleporters)
                teleporter.Draw(gameTime);
            for (int y = 0; y < Height; y++)
                for (int x = 0; x < Width; x++)
                    DrawTile(x, y, gameTime);
        }

        private void DrawTile(int x, int y, GameTime gameTime)
        {
            Vector2 worldPosition = new Vector2(x * TILE_SIZE, y * TILE_SIZE);
            Vector2 screenPosition = Camera.GetScreenPosition(worldPosition);

            if(Tiles[x,y] == TileTypes.Wall)
                Game1.SpriteBatch.Draw(_wallTileImage, screenPosition, null, Microsoft.Xna.Framework.Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.8f);
        }

        public Vector2 GetRandomSpawnWorldPosition()
        {
            int randomIndex = Common.GetRandomNumber(0, _spawnPoints.Count - 1);
            return new Vector2(_spawnPoints[randomIndex].X * TILE_SIZE, _spawnPoints[randomIndex].Y * TILE_SIZE);
        }

        public Vector2 GetRandomTeleporterWorldPosition()
        {
            int randomIndex = Common.GetRandomNumber(0, Teleporters.Count - 1);
            
            return Teleporters[randomIndex].Position;
        }

        public void Update(GameTime gameTime)
        {
            foreach (WeaponPickup pickup in WeaponPickups)
                pickup.Update(gameTime);
            foreach (DestructableWorldItem item in DestructableWorldItems)
                item.Update(gameTime);
            foreach (Teleporter teleporter in Teleporters)
                teleporter.Update(gameTime);
        }

    }
}
